package servlet;

import com.sun.tools.jconsole.JConsoleContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

//@WebServlet(value = "/test",loadOnStartup = 1)
public class TestServlet extends HttpServlet {

    public TestServlet() {
        super();
        System.out.println("TestServlet");
    }


    // alt + insert

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("doPost");

        PrintWriter writer = resp.getWriter();
        ServletOutputStream outputStream = resp.getOutputStream();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet");
        // 设置响应头
        resp.setHeader("key1", "value1");

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
// 通过输出流响应网页数据
        PrintWriter out = resp.getWriter();
        out.println("<h2>");
        out.println(req.getParameter("username"));
        out.println("</h2>");
        out.flush();
        out.close();

        String method = req.getMethod();
        System.out.println(method);
        String url = req.getRequestURL().toString();
        System.out.println(url);
        String pwd = req.getParameter("password");
        System.out.println(pwd);

        String host = req.getHeader("host");
        System.out.println(host);



    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("service");
        // 转发
//        req.setAttribute("key2", "value2");
//        req.getRequestDispatcher("WEB-INF/bootstrap.html").forward(req, resp);

        // 重定向
//        resp.sendRedirect("servletB?key3=value3");
//        resp.sendRedirect("pages/bootstrap.html");

        Cookie cookie = new Cookie("key1", "cookie1");
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);

        Cookie[] cookies = req.getCookies();
        for (Cookie cookie1 : cookies) {
            System.out.println(cookie1.getName());
            System.out.println(cookie1.getValue());
        }

        // session
        HttpSession session = req.getSession();
        session.setAttribute("session1", "sessionValue1");
        session.setAttribute("session1", "sessionValue2");
        String session1 = (String) session.getAttribute("session1");
        System.out.println(session1);


        // ServletContext
        ServletContext servletContext = getServletContext();
        ServletContext servletContext1 = getServletConfig().getServletContext();
        System.out.println(servletContext1 == servletContext);
        System.out.println(servletContext.getContextPath());
        System.out.println(servletContext.getRealPath("/file"));

        String initParameter = servletContext.getInitParameter("key1");
        System.out.println(initParameter);

        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String s = initParameterNames.nextElement();
            String initParameter1 = servletContext.getInitParameter(s);
            System.out.println(s+"  " + initParameter1);
        }

        // ServletConfig
        ServletConfig servletConfig = getServletConfig();
        System.out.println(servletConfig.getServletName());
        String initParameter1 = servletConfig.getInitParameter("servlet1");
        System.out.println(initParameter1);

        Enumeration<String> initParameterNames1 = servletConfig.getInitParameterNames();
        while (initParameterNames1.hasMoreElements()) {
            String s = initParameterNames1.nextElement();
            String initParameter2 = servletConfig.getInitParameter(s);
            System.out.println(s+"  "+initParameter2);
        }
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        System.out.println("init");
        super.init();
    }
}
